package com.iweb.transaction;

import com.iweb.utitls.JdbcUtil;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author ryl
 * @version 1.0
 * @data 2025/4/3 17:11
 * 完成ACID
 */
public class TransactionManager {
    private JdbcUtil util = new JdbcUtil();

    /**
     * 开启事务
     * @return
     * @throws SQLException
     */
    public Connection beginTransaction() throws SQLException {
        Connection conn = util.getConn();
        conn.setAutoCommit(false);
        return conn;
    }

    /**
     * 提交事务
     * @param conn
     * @throws SQLException
     */
    public void commit(Connection conn) throws SQLException {
        conn.commit();
        conn.setAutoCommit(true);
        util.returnConnection(conn);
    }

    /**
     * 事务回滚
     * @param conn
     * @throws SQLException
     */
    public void rollback(Connection conn) throws SQLException {
        conn.rollback();
        conn.setAutoCommit(true);
        util.returnConnection(conn);
    }
}
